<?php

	error_reporting(0);
	session_start();
	error_reporting(1);
	
	require_once "../PinSQL.obj" ;
	$pinSQL = new PinSQL();
	$pinSQL2 = new PinSQL();
     
	// find out if the media has any comments
	$result_comments = mysql_query("SELECT * FROM $gallery->commentsTable WHERE itemid='$itemID' ORDER BY commentdate DESC") or die(mysql_error());
	
	// get number of rows that $result2 returned, into $num2
	$numcomments = 0;
	$numcomments = mysql_numrows($result_comments);
	
	echo "<div class=\"comments_numcomments\">$numcomments comments</div>";
	echo '<div class="paleheader">Comments</div>';
	
	for ($i=0; $i<$numcomments; $i++)
	{
	  $name = mysql_result($result_comments, $i, "name");
	  $visitor = mysql_result($result_comments, $i, "visitor");
	  $email = mysql_result($result_comments, $i, "email");
	  $comment = mysql_result($result_comments, $i, "comment");
	  $commentdate = mysql_result($result_comments, $i, "commentdate");
	  $id = mysql_result($result_comments, $i, "id");
	
	  $prettydate = date('F j, Y', strtotime($commentdate));
	  $prettytime = date('g:ia', strtotime($commentdate));
	  
	  $showdaytime = True;
	  if ($commentdate == "0000-00-00 00:00:00")
	  {
	       $showdaytime = False;
	  }
	  
	  if ($visitor == "")
	  {
	       $visitor = "Anonymous";
	  }
	
?>
	       <div class="horizontal_separation"></div>
	       <div class="comments_header">
<?php
		$pinSQL2->Query("SELECT * FROM members WHERE username='$visitor'");
		$row2 = $pinSQL2->FetchNextRow();
		$visitorID = $row2['id'];
		$pinSQL2->Query("SELECT * FROM memberdata WHERE id='$visitorID'");
		$row2 = $pinSQL2->FetchNextRow();
		$picurl = $row2['picurl'];
		
		if ($picurl)
		{
		$imageSize = getimagesize("../../members/userData/$visitor/$picurl");
		
		if ($imageSize[1] <= 30)
		{
			$newWidth = $imageSize[0];
			$newHeight = $imageSize[1];
		} else {
			$widthFactor = $imageSize[0] / $imageSize[1];
			$newHeight = 30;
			$newWidth = $newHeight * $widthFactor;
		}
		
?>
		<img src="/members/userData/<?php echo $visitor . "/" . $picurl; ?>" width="<?php echo $newWidth; ?>" height="<?php echo $newHeight; ?>" />	
		<div style="position: absolute; left: <?php echo ($newWidth+10); ?>px; top: 2px;">
<?php
		}
?>
	            <a href="/members/frame.php?page=profile&user=<?php echo $visitor; ?>" target="_top"><?php echo $visitor; ?></a>
	            <span class="paleheader_sm">wrote
	            
<?php
	  if ($showdaytime == True)
	  {
	       echo '
	            <br />at ' . $prettytime . ' on ' . $prettydate . '</span>
	            ';
	  }

?>

<?php if ($picurl) {
	?>
			</div>
<?php
}
?>

	       </div>
	       
	       <div class="horizontal_separation"></div>
	       <div class="comment">
	            <?php echo nl2br($comment); ?>
	       </div>
	       
	       <?php if ($_SESSION['level'] == "admin") { ?>
	       <div class="comment_controls">
	            <a href="javascript:DeleteComment(<?php echo $id; ?>)">Delete</a>
	       </div>
	       <?php } ?>
	       
	       
	       <div class="horizontal_separation"></div>

<?php
     }
     
?>